// pages/mainpages/onlineConsultations/onlineConsultations.js
Page({
  data: {
    delBtnWidth:160,
    timer:null,
    data:  [/*{
      doctorId:"01",
      doctorName:"预约取消通知",
      messageText:"ninhiywesdjnsjkj被封杀了孔明借东风后面昆仑山搭街坊不会s",
      messageTime:"2020-5-1 14:30",
      messageUrl:"/pages/images/诊所.png",
      isRight:"未读",
    },
    {
      doctorId:"02",
      doctorName:"预约取消通知",
      messageText:"ninhiywesdjnsjkjvfrgbhnyjumkmjhs",
      messageTime:"2020-5-1",
      messageUrl:"/pages/images/诊所.png",
      isRight:"未读",
    },
    {
      doctorId:"01",
      doctorName:"预约成功通知",
      messageText:"ninhiywesdjnsjkjvfrgbhnyjumkmjhs",
      messageTime:"2020-5-1",
      messageUrl:"/pages/images/15.png",
      isRight:'true',
    },  */
    
  ],
    isScroll:true,
    windowHeight:0,
    pageNum:"3",
  },
  onLoad: function (options) {
    wx.request({
      url:  getApp().globalData.url+'patientsettablebar', // 仅为示例，并非真实的接口地址
      method: 'GET',
      data: {
        unitphone: getApp().globalData.unitphone,
        pageNum:this.data.pageNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
      },
    })
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });
    
  },
  onShow:function(e){
  /*var that=this;
  that.getMessage(); 
   setInterval(function () {
       that.getMessage(); 
      console.log("轮播请求1秒触发一次");
    }, 5000)*/
    this.getMessage();
    let _this = this;
		this.setData({
			timer: setInterval(() => {
        _this.getMessage(); // 这个是我要实时刷新的事件
        console.log("轮播请求1秒触发一次");
			}, 5000)
		})
  },
  getMessage:function(){
    console.log("123")
    var that = this;
    wx.request({
      url:  getApp().globalData.url+'showpatientchatindex', // 仅为示例，并非真实的接口地址
      method: 'GET',
      data: {
        unitphone:getApp().globalData.unitphone
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {  
        let fi={};
        let first1=[];
       for(let i=0;i<res.data.length;i++) 
       {
           var doctorIds="";
           var messageTimes="";
           var doctorNames="";
           var type="";
           var messageTexts="";
           var messageUrls="";
           var messageStates="";
           doctorIds=res.data[i].doctorId;//医生id
           console.log(doctorIds);
           messageTimes=res.data[i].chatTime;//发消息的时间
           //times=res.data[i].messageInform.bookDate + " " + res.data[i].messageInform.bookTime;
           type=res.data[i].chatRecord.flag2;//消息类型（是否为图片）
           doctorNames = res.data[i].doctorName;//医生姓名
           messageStates=res.data[i].readFlag;//消息状态(已读未读)
           messageUrls=res.data[i].doctorHeadLink;//医生照片
           console.log(res.data[i].readFlag)
           console.log(res.data)
           if(type=="1")//最后一条消息
           {
            messageTexts="[图片]";
           }
           else if(type=="0"){
            messageTexts=res.data[i].chatRecord.chatContent;
           }
            console.log(res.data)
          fi={"doctorId":doctorIds,"doctorName":doctorNames,"messageText":messageTexts,"messageTime":messageTimes,"messageUrl":messageUrls,"isRight":messageStates};
          first1.push(fi);
       }
       that.setData({
        "data":first1
      })
      },
      fail(res){
        wx.showToast({
          title: '网络连接失败，请重新尝试',
          icon:'none'
        })
      }
    })
  },
  drawStart: function (e) {
    // console.log("drawStart");  
    var touch = e.touches[0]
    for(var index in this.data.data) {
      var item = this.data.data[index]
      item.right = 0
    }
    this.setData({
      data: this.data.data,
      startX: touch.clientX,
    })
 
  },
  drawMove: function (e) {
    var touch = e.touches[0]
    var item = this.data.data[e.currentTarget.dataset.index]
    var disX = this.data.startX - touch.clientX
    if (disX >= 20) {
      if (disX > this.data.delBtnWidth) {
        disX = this.data.delBtnWidth
      }
      item.right = disX
      this.setData({
        isScroll: false,
        data: this.data.data
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        data: this.data.data
      })
    }
  },  
  toRecords:function(e){
    console.log(e);
    var chooseId=e.currentTarget.dataset.id;
    console.log(chooseId);
    var doctorName =e.currentTarget.dataset.name
    var doctorImg = e.currentTarget.dataset.img
    console.log(doctorName);
    console.log(doctorImg);

    clearInterval(this.data.timer);
		this.setData({
			timer: null
		})
    wx.navigateTo({
       url: '../../../pages/onlineConsultation/onlineConsultation?doctorId='+chooseId + '&&doctorName=' + doctorName + '&&doctorImg=' + doctorImg,
       //url:'../../../pages/onlineConsultation/onlineConsultation?doctorId='+chooseId,
    });
  },
  onUnload:function(){
    clearInterval(this.data.timer);
		this.setData({
			timer: null
		})
  },
  onHide:function(){
    clearInterval(this.data.timer);
		this.setData({
			timer: null
		})
  },
  drawEnd: function (e) {
    var item = this.data.data[e.currentTarget.dataset.index]
    if (item.right >= this.data.delBtnWidth/2) {
      item.right = this.data.delBtnWidth
      this.setData({
        isScroll: true,
        data: this.data.data,
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        data: this.data.data,
      })
    }
  },
  delItem: function (e) {
    this.data.data.splice(e.currentTarget.dataset.index, 1)
    var doctorIds=e.currentTarget.dataset.id
    console.log(doctorIds)
    this.setData({
     data: this.data.data
    })
    wx.request({
      url:  getApp().globalData.url + 'deletepatientonechatindex',
      method: 'GET',
      data: {
        tel: getApp().globalData.unitphone,
        //otherTel: that.data.otherTel,
        doctorId:doctorIds
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'  // 默认值
      },
      success(res){
          wx.showToast({
            title: '成功删除聊天记录',
            icon: 'none',
        })   
      },
      fail(res){
        wx.showToast({
          title: '成功删除聊天记录',
          icon: 'none',
      })   
      }
    })
  }
})